1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
/*!
A dependency between two values
*/
use crate::value::*;

/// Dependency metadata for a symbol
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct Dependency {
    /// The variance of this value with respect to a free variable's value
    pub variance: Variance,
    /// The relationship between this value and a symbol
    pub relationship: Relationship,
}

impl PartialOrd for Dependency {
    #[inline]
    fn partial_cmp(&self, other: &Dependency) -> Option<Ordering> {
        self.const_cmp(*other)
    }
}

impl Debug for Dependency {
    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
        if *self == Dependency::CONTRADICTION {
            write!(fmt, "!")
        } else {
            write!(fmt, "SymDep({:?}, {:?})", self.variance, self.relationship)
        }
    }
}

impl Dependency {
    /// A fully consumed dependency
    pub const USED: Dependency = Dependency::covar(Usage::USED);
    /// A contradictory dependency
    pub const CONTRADICTION: Dependency = Dependency {
        variance: Invariant,
        relationship: Relationship::CN,
    };
    /// A null dependency
    pub const NULL: Dependency = Dependency {
        variance: Bivariant,
        relationship: Relationship::TV,
    };
    /// Get the covariant symbol dependency of a given usage
    #[inline]
    pub const fn covar(usage: Usage) -> Dependency {
        Dependency {
            variance: Covariant,
            relationship: Relationship::dependency(usage),
        }
    }
    /// Constant-compare two dependencies
    #[inline]
    pub const fn const_cmp(self, other: Dependency) -> Option<Ordering> {
        lattice_ord_opt(
            other.variance.const_cmp(self.variance),
            self.relationship.const_cmp(other.relationship),
        )
    }
    /// Flip a dependency
    #[inline]
    pub fn flip(self) -> Dependency {
        Dependency {
            variance: self.variance.flip(),
            relationship: self.relationship.flip(),
        }
    }
    /// Take a given dependency with respect to a given symbol
    #[inline]
    pub fn of(self, symbol: &SymbolId) -> Dependency {
        Dependency {
            variance: self.variance.of(symbol),
            relationship: self.relationship.of(symbol),
        }
    }
    /// Take the join of two dependency kinds
    #[inline]
    pub fn join(self, other: Dependency) -> Dependency {
        Dependency {
            variance: self.variance.join(other.variance),
            relationship: self.relationship.join(other.relationship),
        }
    }
    /// Take the meet of two dependency kinds
    #[inline]
    pub fn meet(self, other: Dependency) -> Dependency {
        Dependency {
            variance: self.variance.meet(other.variance),
            relationship: self.relationship.meet(other.relationship),
        }
    }
    /// Iterate over all possible symbol dependencies
    #[inline]
    pub fn iter_all() -> impl Iterator<Item = Dependency> {
        RELATIONSHIPS
            .iter()
            .copied()
            .map(|relationship| {
                VARIANCES.iter().copied().map(move |variance| Dependency {
                    variance,
                    relationship,
                })
            })
            .flatten()
    }
}